(() => {
	

	let ele = {};


	ele.sellBox = $('#sellBox');
	ele.options = $('#tab-team li')
	ele.noticeul = $('#noticeul');//滚动通知栏
	ele.reward_list = $('#reward_list');//奖励列表

	ele.empty = $('#empty');

	ele.modal_explian = $('#modal-explian');
	ele.close_explian = $('#close-explian');
	
	var timer

	let dblist = [
		{
			id:0,
			page:1,
			isrequest:true,
			name:'all',
			type:'',
		},
		{
			id:1,
			page:1,
			isrequest:false,
			name:'0',
			type:0,
		},
		{
			id:2,
			page:1,
			isrequest:false,
			name:'1',
			type:1,
		},
		{
			id:3,
			page:1,
			isrequest:false,
			name:'2',
			type:2,
		},
	]

	let newTb = new Tabbar({
		tabId: 'tab-new',
		titleClass: 'new-title',
		contentClass: 'new-item',
		activeClass: 'active',
		callBack: (res) => {
			// console.log(res);
			if(!dblist[res.active].isrequest) {
				getlist2(dblist[res.active])
			}
		}
	});



	const typeList = ['', 'sell', 'buy', 'reward',];
	let typeName = 'all';
	let currentPage = {
		sell: 1,
		buy: 0,
		reward: 2,
	}

	treasuereList();

	getlist(1,'reward')
	tabBar();

	let list = ''


	//关闭 说明
	// ele.close_explian.onclick = () => {
	// 	closeMdl(ele.modal_explian);
	// }

	// 夺宝
	function treasuereList() {
		let token = getCookie("token");
		ajax({
			url: API_PATH.TREASURE_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			success: res => {
				if (res.code == 1) {
					list = res.data.list
					drawTreasuereList(res.data.list);

				} else openTips(res.msg || '未知错误');
			}
		});
	}


	// 渲染页面
	function drawTreasuereList(_data) {
		let html = '';
		for (let i = 0; i < _data.length; i++) {
			let hasnum = (_data[i].has_num / _data[i].need_num) * 100 + '%'
			html += '<div class="sellin">'
				+ '<div class="selltop">'
				+ '<p class="qishu">第' + _data[i].qi + '期数</p>'
				+ '<img src="http://img.qxgs.cc/tiantianlaishouzu/img/seize/'+_data[i].id+'.png" alt="">'
				+ '</div>'
				+ '<div class="sellbtm">'
				+ '<div class="sellbtm1">'
				+ '<p>' + _data[i].name + '× '+_data[i].num+'</p>'
				+ '<div class="sellbtm2">'
				+ '<p></p>'
				+ '<span style="width:' + hasnum + '"></span>'
				+ '</div>'
				+ '</div>'
				+ '<button class="sellbtm3" data="' + _data[i].id + '">参与</button>'
				+ '</div>'
				+ '</div>'
		}

		ele.sellBox.innerHTML += html
		navto()
	}

	function navto() {
		let tz = $('.sellbtm3')
		for (let i = 0; i < tz.length; i++) {
			tz[i].onclick = () => {
				let id = tz[i].getAttribute('data')
				location.href = 'kidbaby.html?id=' + id
			}
		}
	}


	// 选项卡选项功能
	function tabBar() {
		for (let i = 0; i < ele.options.length; i++) {
			ele.options[i].addEventListener('click', e => {
				let cls = e.target.className;
				// console.log(cls);
				typeName = cls.split(' ')[2].split('-')[1];
				if (currentPage[typeName] == 0) {
					currentPage[typeName]++;
					getlist2(dblist[0])
				}else if (currentPage[typeName] == -1) {
					ele.empty.style.display = 'block';
				}
			})
		}
	}


	// 比赛记录  中奖名单
	function getlist(page,moretype) {
		let more = $('.more-' + moretype)[0]
		// console.log(more);
		let token = getCookie("token");
		ajax({
			url: API_PATH.RECORD_LIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: page,
			},
			success: res => {
				if (res.code == 1) {
					let detail = res.data.data;
					drawnotice(res.data.data)
					if (detail.length === 0) {
						currentPage[typeName] = -1;
						more.style.display = "none";
						ele.empty.style.display = "block";
					} else {
						drawreward(detail);
						page++;
						if (page > res.data.last_page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						}else {
							more.innerText = '加载更多';
							more.onclick = () => getlist(page,moretype);
						}
					}
					
				} else openTips(res.msg || '未知错误');
			}
		});
	}

	// 渲染中奖列表

	function drawreward(data){
		let html = ''
		for (let i = 0; i < data.length; i++) {
			let d = data[i]
			let headerimg = ''
			if(d.agent.detail.head_pic == '' || d.agent.detail.head_pic == null) {
                if(d.agent.detail.sex == 1) {
                    headerimg = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png'
                }else {
                    headerimg = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png'
                }
            }else {
                headerimg = IMG_DOMAIN + d.agent.detail.head_pic
            }
			 html += '<div class="liebiao">'+
							'<div class="lieLeft">'+
							'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/seize/'+d.hd_info.id+'.png" alt="">'+
							'</div>'+
							'<div class="lieCenter">'+
								'<p>'+d.hd_info.name+'</p>'+
								'<span>第'+d.num+'期</span>'+
							'</div>'+
							'<div class="lieRight">'+
							'<img src="'+headerimg+'" alt="">'+
								'<span>'+d.agent.detail.nick_name+'</span>'+
							'</div>'+
						'</div>';
		}
		ele.reward_list.innerHTML += html
	}


	// 绘制滚动通知栏
	function drawnotice(data){
		let html = '';
		for (let i = 0; i < data.length; i++) {
			let d = data[i]
			let headerimg = ''
			if(d.agent.detail.head_pic == '' || d.agent.detail.head_pic == null) {
                if(d.agent.detail.sex == 1) {
                    headerimg = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png'
                }else {
                    headerimg = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png'
                }
            }else {
                headerimg = IMG_DOMAIN + d.agent.detail.head_pic
            }
			html += '<li>'+
						'<div class="notice">'+
							'<img class="labaimg" src="http://img.qxgs.cc/tiantianlaishouzu/img/seize/laba.png" alt="">'+
							'<div class="noticein">'+
								'<img src="'+headerimg+'" alt="">'+
								'<p>恭喜'+d.agent.detail.nick_name+'获得'+d.hd_info.name+'地段地图</p>'+
							'</div>'+
						'</div>'+
					'</li>';
		}
		ele.noticeul.innerHTML += html
		clearInterval(timer)
		noticestart();
	}


	

	// 参与记录 夺宝订单
	function getlist2(data) {
		// console.log(data);
		let more = $('.more-'+ data.name)[0]
		let empty = $('.empty-' + data.name)[0]

		let token = getCookie("token");
		ajax({
			url: API_PATH.CYRECORDLIST,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: data.page,
				state:data.type
			},
			success: res => {
				if (res.code == 1) {
					let detail = res.data.data;
					if (detail.length === 0) {
						more.style.display = "none";
						empty.style.display = "block";
					} else {
						dblist[data.id].isrequest = true
						drawalog(detail , data)
						dblist[data.id].page++
						if (dblist[data.id].page > res.data.last_page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						}
						else {
							more.innerText = '加载更多';
							more.onclick = () => getlist2(dblist[data.id]);
						}
					}
				} else openTips(res.msg || '未知错误');
			}
		});
	}


	function drawalog(detail , data) {
		// console.log(detail);
		let list = $('#item-' + data.name)
		let html = ''
		for (let i = 0; i < detail.length; i++) {
			let d = detail[i];
			let state = d.state ==0?'green':d.state ==1?'':d.state ==2?'red':''
			let statetext = d.state ==0?'待开奖':d.state ==1?'未中奖':d.state ==2?'中奖了':''

			html += '<div class="liebiao">'+
						'<div class="lieLeft">'+
							'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/seize/'+d.hd_info.id+'.png" alt="">'+
						'</div>'+
						'<div class="lieCenter">'+
							'<p>'+d.hd_info.name+'</p>'+
							'<span>第'+d.num+'期</span>'+
						'</div>'+
						'<div class="lieRight">'+
							'<p class="'+state+'">'+statetext+'</p>'+
						'</div>'+
					'</div>';
		}

		list.innerHTML += html
	}




	// 滚动开始
	function noticestart(){
		let box =  $('#scroll-box')
		let parent = $('ul')[0]
		let parent2 = $('ul')[1]
		parent2.innerHTML = parent.innerHTML
		/*启动定时器*/
		clearInterval(timer)
		timer = setInterval(autoScrollLine, 30)
		/*单行向上滚动效果*/
		function autoScrollLine() {
			/*判断滚动内容是否已经滚完，滚完了则滚动的值重新设置到0
			否则就每隔30毫秒向上滚动1px*/
			if (box.scrollTop >= parent.offsetHeight) {
				box.scrollTop = 0;
			} else {
				box.scrollTop++;
			}
			/*判断滚动的距离刚好为一条公告的高度时停掉定时器，
			隔1s之后重新启动定时器即可实现公告滚动停留效果 */
			if (box.scrollTop % box.offsetHeight == 0) {
				clearInterval(timer)
				setTimeout(() => {
					timer = setInterval(autoScrollLine, 30)
				}, 1000)
			}
		}
	}
	



})()